<template>
  <div class="main-content">
    <el-card style="width: 50%; margin: 30px auto">
      <div style="text-align: right; margin-bottom: 20px">
        <el-button type="primary" @click="updatePassword">修改密码</el-button>
      </div>
      <el-form :model="user" label-width="80px" style="padding-right: 20px">
        <el-form-item>
          <el-upload style="margin-left: 50px;" class="avatar-uploader" action="http://localhost:8080/user/upload"
            :headers="{ Authorization: token }" name="file" :show-file-list="false" :on-success="handleSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="user.img" :src="user.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.userName" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="user.phone" placeholder="电话"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="user.role" placeholder="角色" disabled></el-input>
        </el-form-item>
        <el-form-item label="服务时长" prop="serviceDuration">
          <el-input v-model="user.serviceDuration" placeholder="服务时长" disabled></el-input>
        </el-form-item>
        <div style="text-align: center; margin-bottom: 20px">
          <el-button type="primary" @click="update">保 存</el-button>
        </div>
      </el-form>
    </el-card>
    <!-- <el-dialog title="修改密码" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="user" label-width="80px" style="padding-right: 20px" :rules="rules" ref="formRef">
        <el-form-item label="原始密码" prop="password">
          <el-input show-password v-model="user.password" placeholder="原始密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input show-password v-model="user.newPassword" placeholder="新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input show-password v-model="user.confirmPassword" placeholder="确认密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { getUserInfoService, updateUserInfoService } from '@/api/front/common'
import { message, messageType } from '@/utils/message';
export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value !== this.user.newPassword) {
        callback(new Error('确认密码错误'))
      } else {
        callback()
      }
    }
    return {
      // user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      dialogVisible: false,
      imageUrl:'',
      rules: {
        password: [
          { required: true, message: '请输入原始密码', trigger: 'blur' },
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
        ],
        confirmPassword: [
          { validator: validatePassword, required: true, trigger: 'blur' },
        ],
      },
      user: {
        userName: '',
        phone: '',
        img: '',
        role: '',
        serviceDuration: '',
      },
      token:'',
    }
  },
  created() {
    this.getUserInfo();
    this.getToken();
  },
  methods: {
    getToken() {
      this.token = "Bearer " + localStorage.getItem('edb-authorization-token');
    },
     getUserInfo() {
      // 获取当前用户的信息
      getUserInfoService().then(resp => {
        if (!resp) return;
        console.log('resp-->', resp.data);
        if (resp.data.data['role'] == 2) {
                    resp.data.data['role']='管理员'
        }else {
          resp.data.data['role'] = '普通用户'
        }

        this.user = resp.data.data;
        console.log('this.user', this.user);
      }, error => {
        console.log(error);
       })
    },
    handleSuccess(resp) {
      console.log(resp);
      this.user.img = resp.data;
    },
    beforeAvatarUpload() { },
    update() {
      if (this.user.role == '管理员') {
        this.user.role =2
      } else {
        this.user.role = 1
      }
      // 保存当前的用户信息到数据库
      updateUserInfoService(this.user).then(resp => {
        if (!resp) return;
        message(messageType().success, '保存成功');
      }, error => {
        console.log(error);
      })
    },
    handleAvatarSuccess(response) {
      // 把user的头像属性换成上传的图片的链接
      this.$set(this.user, 'avatar', response.data)
    },
    // 修改密码
    updatePassword() {
      this.dialogVisible = true
    },
    save() {
      /* this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request.put('/updatePassword', this.user).then(res => {
            if (res.code === '200') {
              // 成功更新
              this.$message.success('修改密码成功')
              this.$router.push('/login')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      }) */
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #050;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.main-content{
  height: 600px;
}
</style>